//
//  SchoolViewController.m
//  ZYShang
//
//  Created by wangguangming on 15/8/12.
//  Copyright © 2015年 Amber. All rights reserved.
//

#import "SchoolViewController.h"
#import "Base.h"
#import "ReadyDiSignle.h"
#import "School.h"
#import "WorkerViewController.h"
@interface SchoolViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    BOOL _isqingqiu;
}
@property(nonatomic,strong)UITableView *mainTable;
@property(nonatomic,strong)UITableView *schoolTable;
@property(nonatomic,retain)id<PassTrendValueDelegate>trendDelegate;
@end

@implementation SchoolViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _isqingqiu=YES;
    self.view.backgroundColor=COLOR;
    self.title=@"选择学校";
    self.mainTable=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH/4-1, HEIGHT)];
   
    self.mainTable.backgroundColor=COLOR;
    
    [self.view addSubview:self.mainTable];
    self.mainTable.delegate=self;
    self.mainTable.dataSource=self;
    self.mainTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [[ReadyDiSignle signle] getAllCitys];
  
    self.schoolTable=[[UITableView alloc] initWithFrame:CGRectMake(WIDTH/4+1, 64, WIDTH*3/4-1, HEIGHT-64)];
    [self.view addSubview:self.schoolTable];
    self.schoolTable.backgroundColor=COLOR;
    self.schoolTable.delegate=self;
    self.schoolTable.dataSource=self;
     self.schoolTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [[ReadyDiSignle signle] addObserver:self forKeyPath:@"cityArray" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
   
    // Do any additional setup after loading the view.
}
-(void)observeValueForKeyPath:(nullable NSString *)keyPath ofObject:(nullable id)object change:(nullable NSDictionary *)change context:(nullable void *)context
{
    if ([keyPath isEqualToString:@"cityArray"]) 
        {
        [self.mainTable reloadData];
        if (_isqingqiu) {
            NSIndexPath *index=[NSIndexPath indexPathForRow:0 inSection:0];
            [self tableView:self.mainTable didSelectRowAtIndexPath:index];
            _isqingqiu=NO;
        }
               
        [[ReadyDiSignle signle] removeObserver:self forKeyPath:@"cityArray"];
        }
    if ([keyPath isEqualToString:@"schoolArray"]) 
        {
        [self.schoolTable reloadData];
         [[ReadyDiSignle signle] removeObserver:self forKeyPath:@"schoolArray"];
       }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView==self.mainTable)
        {
        return [ReadyDiSignle signle].cityArray.count;

       }
    else
        {
        return [ReadyDiSignle signle].schoolArray.count;
        }
}

  
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView==self.mainTable) {
        static NSString *cellid=@"citycellid";
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellid]; 
        if (cell==nil)
            {
            cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
            }
        cell.textLabel.text=[ReadyDiSignle signle].cityArray[indexPath.row];
        UILabel *l=[[UILabel alloc] initWithFrame:CGRectMake(5, 42, WIDTH/4-10, 2)];
        l.backgroundColor=[UIColor whiteColor];
        [cell addSubview:l];
        cell.backgroundColor=COLOR;
        UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH/4-1, 44)];
        view.backgroundColor=[UIColor whiteColor];
        cell.selectedBackgroundView=view;
        return cell;

    }
    else
        {
        School *school=[ReadyDiSignle signle].schoolArray[indexPath.row];
        static NSString *cellid=@"schoolcellid";
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellid]; 
        if (cell==nil)
            {
            cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
            }
        cell.textLabel.text=school.name;
        UILabel *l=[[UILabel alloc] initWithFrame:CGRectMake(5, 42, WIDTH*3/4-10, 2)];
        l.backgroundColor=COLOR;
        [cell addSubview:l];
        return cell;

        }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    if (tableView==self.mainTable)
        {
        [ReadyDiSignle signle].oidString=[ReadyDiSignle signle].cityArray[indexPath.row];
        [[ReadyDiSignle signle] getallSchool];
        [[ReadyDiSignle signle] addObserver:self forKeyPath:@"schoolArray" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
        
        }
    else
        {
        School *school=[ReadyDiSignle signle].schoolArray[indexPath.row];
        WorkerViewController *work=[[WorkerViewController alloc] init];
        self.trendDelegate=work;
        
        [self.trendDelegate passTrendValues:school.mktId];

        [self.navigationController popViewControllerAnimated:YES];

        }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
